<?php

namespace app\common\service\StoreService;

use app\model\BbcModel\ChannelBusiness;
use app\model\BbcModel\Member;
use app\model\BbcModel\ServiceProvider;
use app\model\BbcModel\Store;
use app\model\BbcModel\VipOrder;
use app\model\BbcModel\VipSetting;
use think\facade\Db;
use app\Normal;
use app\model\BbcModel\Channel;



/**
 * 分销商业务
 * @author linan
 */
class VipService
{
    /**
     * 会员价格及分佣
     * @param array $param
     * @return array
     */
    public function editSetting( array $param )
    {
        if(empty($param['cid'])){
            return Normal::returnCode(Normal::FAIL,"请选择所属大B");
        }
        $channel=Channel::instance()->info(['id'=>$param['cid']]);
        if(empty($channel)){
            return Normal::returnCode(Normal::FAIL,"所选择的大B不存在");
        }
        $has=VipSetting::instance()->info(['cid'=>$param['cid']]);
        $year_activity=0;
        $quarter_activity=0;
        $month_activity=0;
        if(empty($param['year'])){
            return Normal::returnCode(Normal::FAIL,"请输入年卡价格");
        }
        if(!empty($param['year_activity'])){
            if($param['year_activity']>$param['year']){
                return Normal::returnCode(Normal::FAIL,"年卡特价不能大于年卡价格");
            }
            $year_activity=$param['year_activity'];
        }
        if(empty($param['quarter'])){
            return Normal::returnCode(Normal::FAIL,"请输入季卡价格");
        }
        if(!empty($param['quarter_activity'])){
            if($param['quarter_activity']>$param['quarter']){
                return Normal::returnCode(Normal::FAIL,"季卡特价不能大于季卡价格");
            }
            $quarter_activity=$param['quarter_activity'];
        }
        if(empty($param['month'])){
            return Normal::returnCode(Normal::FAIL,"请输入月卡价格");
        }
        if(!empty($param['month_activity'])){
            if($param['month_activity']>$param['month']){
                return Normal::returnCode(Normal::FAIL,"月卡特价不能大于月卡价格");
            }
            $month_activity=$param['month_activity'];
        }
        $master=$param['master']??0;
        $shop=$param['shop']??0;
        $store=$param['store']??0;
        if($master+$shop+$store<>100){
            return Normal::returnCode(Normal::FAIL,"分佣比例之和不等于100");
        }
        $data=[
            'cid'=>$param['cid'],
            'year'=>$param['year'],
            'year_activity'=>$year_activity,
            'quarter'=>$param['quarter'],
            'quarter_activity'=>$quarter_activity,
            'month'=>$param['month'],
            'month_activity'=>$month_activity,
            'master'=>$master,
            'shop'=>$shop,
            'store'=>$store,
            'md'=>time()
        ];
        if($has){
            $res=VipSetting::instance()->edit(['id'=>$has['id']],$data);
            if($res){
                return Normal::returnCode(Normal::SUC,"编辑成功");
            }else{
                return Normal::returnCode(Normal::FAIL,"编辑失败");
            }
        }else{
            $res=VipSetting::instance()->add($data);
            if($res){
                return Normal::returnCode(Normal::SUC,"新增成功");
            }else{
                return Normal::returnCode(Normal::FAIL,"新增失败");
            }
        }
    }

    /**
     * 会员价格设置
     */
    public function getVipSetting(array $param )
    {
        $has=VipSetting::instance()->info(['cid'=>$param['cid']]);
        return Normal::returnCode(Normal::SUC,"请求成功",$has);
    }

    /**
     * 组装查询条件
     * @param array $param
     * @return array
     */
    public function makeWhere(array $param)
    {
        $where=[];
        //服务商id
        if(!empty($param['sp_id'])){
            $where[]=['sp_id','=',$param['sp_id']];
        }
        //大Bid
        if(!empty($param['cid'])){
            $where[]=['cid','=',$param['cid']];
        }
        //渠道商id
        if(!empty($param['cb_id'])){
            $where[]=['cb_id','=',$param['cb_id']];
        }
        //店铺id
        if(!empty($param['mn_sid'])){
            $where[]=['mn_sid','=',$param['mn_sid']];
        }
        //子订单号
        if(!empty($param['sn'])){
            $where[]=['sn','like','%'.$param['sn'].'%'];
        }
        if(!empty($param['mobile'])){
            $where[]=['mobile','like','%'.$param['mobile'].'%'];
        }
        //下单时间
        if(!empty($param['cd_start'])){
            $where[]=['cd','>=',strtotime($param['cd_start'])];
        }
        if(!empty($param['cd_end'])){
            $where[]=['cd','<=',strtotime($param['cd_end'])];
        }
        //支付时间
        if(!empty($param['pay_start'])){
            $where[]=['pay_time','>=',strtotime($param['pay_start'])];
        }
        if(!empty($param['pay_end'])){
            $where[]=['pay_time','<=',strtotime($param['pay_end'])];
        }
        //服务商列表
        $service=ServiceProvider::instance()->lists(['_fields_'=>'id,name']);
        $service=array_column($service,'name','id');
        //渠道商列表
        $business=ChannelBusiness::instance()->lists(['_fields_'=>'id,name']);
        $business=array_column($business,'name','id');
        //店铺列表
        $store=Store::instance()->lists(['_fields_'=>'id,store_name']);
        $store=array_column($store,'store_name','id');
        return [
            'where'=>$where,
            'service'=>$service,
            'business'=>$business,
            'store'=>$store,
        ];
    }

    /**
     * 会员订单列表
     * @param array $param
     * @return array
     */
    public function vipOrderList( array $param)
    {
        $check=$this->makeWhere($param);
        $page=(isset($param['page'])&&$param['page']>0)?$param['page']:1;
        $limit=(isset($param['limit'])&&$param['limit']>0)?$param['limit']:10;
        $p=($page-1)*$limit;
        $where=$check['where'];
        $service=$check['service'];
        $business=$check['business'];
        $store=$check['store'];
        $list=VipOrder::instance()
            ->where($where)
            ->limit($p, $limit)
            ->order('id desc')
            ->select()
            ->toArray();
        $num=VipOrder::instance()
            ->where($where)
            ->count();
        $data['total']=$num;
        foreach ($list as $k=>$v){
            $list[$k]['type_name']=VipOrder::instance()->type[$v['type']];
            $list[$k]['store_name']=$store[$v['mn_sid']];
            $list[$k]['channel_name']=$business[$v['cb_id']];
            $list[$k]['service_name']=$service[$v['sp_id']];
            $list[$k]['cd']=$v['cd']>0?date('Y-m-d H:i:s',$v['cd']):'';
            $list[$k]['pay_time']=$v['pay_time']>0?date('Y-m-d H:i:s',$v['pay_time']):'';
        }
        $data['list']=$list;
        return Normal::returnCode(Normal::SUC,"请求成功",$data);
    }

    /**
     * 会员订单详情
     * @param array $param
     * @return array
     */
    public function vipOrderDetail( array $param)
    {
        if(empty($param['sn'])){
            return Normal::returnCode(Normal::FAIL,"缺少参数sn");
        }
        $info=VipOrder::instance()->info(['sn'=>$param['sn']]);
        if(empty($info)){
            return Normal::returnCode(Normal::FAIL,"订单不存在");
        }
        $info['type_name']=VipOrder::instance()->type[$info['type']];
        $info['store_name']=Store::instance()->where(['id'=>$info['mn_sid']])->value('store_name');
        $info['channel_name']=ChannelBusiness::instance()->where(['id'=>$info['cb_id']])->value('name');
        $info['service_name']=ServiceProvider::instance()->where(['id'=>$info['sp_id']])->value('name');
        $info['cd']=$info['cd']>0?date('Y-m-d H:i:s',$info['cd']):'';
        $info['pay_time']=$info['pay_time']>0?date('Y-m-d H:i:s',$info['pay_time']):'';
        return Normal::returnCode(Normal::SUC,"请求成功",$info);
    }

}
